package com.vancouver.knows.business.tracker;

import java.util.List;

import com.pangu.service.ActionException;
import com.pangu.task.IResultReceiver;
import com.pangu.task.OperateResult;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.Constants;
import com.vancouver.knows.business.bean.CityDiscountDetailInfo;
import com.vancouver.knows.business.bean.CityNewsDetailInfo;
import com.vancouver.knows.business.bean.CityNewsInfo;
import com.vancouver.knows.business.bean.CommentInfo;
import com.vancouver.knows.business.bean.UserPostDetailInfo;
import com.vancouver.knows.business.task.mark.UserUpvoteTaskMark;

/**
 *  用户点赞
 * @author chenxunlin01
 * 
 */
public class UserUpvoteTracker extends ABInvokeTracker {

	public UserUpvoteTracker(BusinessModule module, IResultReceiver resultReceiver) {
		super(module, resultReceiver);
	}

	@Override
	public String TAG() {
		return UserUpvoteTracker.class.getSimpleName();
	}

	@Override
	public void handleResult(OperateResult result) {
		updateUpvoteStatus(result, false);
	}

	private void updateUpvoteStatus(OperateResult result, boolean isUpvoteStatus) {
		UserUpvoteTaskMark taskMark = (UserUpvoteTaskMark) result.getTaskMark();
		if ("post".endsWith(taskMark.getTargetType())) {
			CityNewsDetailInfo cityNewsDetailInfo = module.getCacheManager().getCityNewsDetailInfoMap()
					.get(taskMark.getTargetId());
			if (cityNewsDetailInfo != null) {
				cityNewsDetailInfo.setUserUpvoted("Y");
				if (!isUpvoteStatus) {
					cityNewsDetailInfo.setUpvoteCount(cityNewsDetailInfo.getUpvoteCount() + 1);
				}
			}

			CityNewsInfo cityNewsInfo = module.getCacheManager().getCityNewsInfoCache()
					.getItemInfoById(taskMark.getTargetId());
			if (cityNewsInfo != null) {
				cityNewsInfo.setUserUpvoted("Y");
				if (!isUpvoteStatus) {
					cityNewsInfo.setUpvoteCount(cityNewsInfo.getUpvoteCount() + 1);
				}
			}

			CityDiscountDetailInfo cityDiscountDetailInfo = module.getCacheManager().getDiscountDetailInfoMap()
					.get(taskMark.getPostId());
			if (cityDiscountDetailInfo != null) {
				cityDiscountDetailInfo.setUserUpvoted("Y");
				if (!isUpvoteStatus) {
					cityDiscountDetailInfo.setUpvoteCount(cityDiscountDetailInfo.getUpvoteCount() + 1);
				}
			}

			imContext.handleMobEmptyMessage(Constants.BUSINESS_STATUS_USER_UPVOTE);

		} else if ("comment".endsWith(taskMark.getTargetType())) {
			CityNewsDetailInfo cityNewsDetailInfo = module.getCacheManager().getCityNewsDetailInfoMap()
					.get(taskMark.getPostId());
			if (cityNewsDetailInfo != null) {
				List<CommentInfo> commentInfoList = cityNewsDetailInfo.getCommentInfoList();
				setUpvoted(commentInfoList, "Y", taskMark.getTargetId());
			}

			List<CommentInfo> commentInfoList = module.getCacheManager().getCommentInfoMap().get(taskMark.getPostId());
			setUpvoted(commentInfoList, "Y", taskMark.getTargetId());

			CityDiscountDetailInfo cityDiscountDetailInfo = module.getCacheManager().getDiscountDetailInfoMap()
					.get(taskMark.getPostId());
			if (cityDiscountDetailInfo != null) {
				List<CommentInfo> commentInfos = cityDiscountDetailInfo.getCommentInfoList();
				setUpvoted(commentInfos, "Y", taskMark.getTargetId());
			}

			UserPostDetailInfo userPostDetailInfo = module.getCacheManager().getUserPostDetailInfoCache().get(taskMark.getPostId());
			if (userPostDetailInfo != null) {
				setUpvoted(userPostDetailInfo.getCommentInfoList(), "Y", taskMark.getTargetId());
			}

			imContext.handleMobEmptyMessage(Constants.BUSINESS_STATUS_USER_COMMENT_UPVOTE);
		}
	}

	@Override
	public void handleFault(OperateResult result) {
		super.handleFault(result);

		ActionException actionException = result.getActionException();
		if (actionException.getExCode() == 4) {
			updateUpvoteStatus(result, true);
		}
	}

	private void setUpvoted(List<CommentInfo> commentInfoList, String upvoted, String commentId) {
		if (commentInfoList != null) {
			for (CommentInfo commentInfo : commentInfoList) {
				if (commentId.equals(commentInfo.getId())) {
					commentInfo.setUpvoted(upvoted);
					commentInfo.setUpvoteCount(commentInfo.getUpvoteCount() + 1);
					break;
				}
			}
		}
	}
}
